package com.xwaf.platform.system.auth.serviceimpl;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.service.service.CrudService;
import com.xwaf.platform.system.api.entity.auth.ImportLog;
import com.xwaf.platform.system.api.entity.auth.User;
import com.xwaf.platform.system.api.enums.ImportStatus;
import com.xwaf.platform.system.api.enums.ImportType;
import com.xwaf.platform.system.api.service.auth.ImportLogService;
import com.xwaf.platform.system.api.service.pay.OrderService;
import com.xwaf.platform.system.auth.dao.ImportLogDao;
import com.xwaf.platform.system.auth.dao.UserDao;
import com.xwaf.platform.system.auth.task.data.ImportData;
import com.xwaf.platform.system.auth.task.data.Pool;

/**
 * 
 * <p>
 * 导入日志 rquest对象
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2020-1-4 14:13:56 <br>
 * @see 无<br>
 *      Copyright (c) 2020, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Transactional(readOnly = true)
@Service("importLogService")
public class ImportLogServiceImpl extends CrudService<ImportLogDao, ImportLog> implements ImportLogService {
	@Autowired
	private ImportLogDao importLogDao;

	@Autowired
	@Qualifier(value = "orderExecutor")
	ThreadPoolTaskExecutor orderExecutor;

	@Autowired
	ApplicationContext ctx;

	@Autowired
	private UserDao userDao;

	@Override
	public Page<ImportLog> selPageList(Page<ImportLog> page, ImportLog importLog) {
		List<ImportLog> list = importLogDao.selPageList(page, importLog);
		page.setRecords(list);
		return page;
	}

	@Override
	public int importOrder(ImportLogService importLogService, OrderService orderService, ImportLog importLog,
			ELUser loginUser, String filePath, String fileName, String saveErrorUrl, String saveErrorFile,
			Locale local) {
		User u = userDao.selectById(loginUser.getId());
		importLog.setFileName(importLog.getFileName());
		importLog.setFilePath(importLog.getFilePath());// 相对路径
		importLog.setErrorFileName("ERROR-" + saveErrorFile);
		importLog.setErrorFilePath(importLog.getFilePath().substring(0, importLog.getFilePath().lastIndexOf("/"))
				+ "/error/" + "ERROR-" + saveErrorFile);
		importLog.setCompanyId(loginUser.getCompanyId());
		importLog.setUploadTime(new Date());
		importLog.setStatus(ImportStatus.WAIT);
		importLog.setUserId(loginUser.getId());
		importLog.setUserActualName(u.getActualName());
		importLog.setType(ImportType.ORDER);
		int num = importLogService.insert(importLog) ? 1 : 0;
		List<ImportData> seDatas = Pool.get(loginUser.getCompanyId());
		ImportData importData = new ImportData(importLog.getId(), filePath, fileName, saveErrorUrl, saveErrorFile,
				loginUser, ctx, local, orderExecutor, importLog.getType().name());
		seDatas.add(importData);
		return num;
	}
}
